﻿using UnityEngine;
using System.Collections;

public class SkillManager : MonoBehaviour {

    public static SkillManager _instance;

    private int foodAttack;
    private int foodDodge;
    private Food tempFood;

    void Awake()
    {
        _instance = this;
    }


    public void UseSkill(Food food, string tag)
    {
        tempFood = food;
        foodAttack = food.foodInfo.AttackForce;
        //foodDodge = food.dodge;

        switch (food.skillInfo.skilltype)
        {
            case SkillType.Attack:
                AttackSkill(food, tag);
                break;
            case SkillType.Health:
                HealthSkill(food, tag);
                break;
            case SkillType.Passive:
                PassiveSkill(food, tag);
                break;
            default:
                AttackSkill(food, tag);
                break;
        }
    }


    public void BuffClear()
    {
        tempFood.foodInfo.AttackForce = foodAttack;
        //tempFood.dodge = foodDodge;
    }

    /// <summary>
    /// 统一处理是否可以释放技能
    /// </summary>
    /// <param name="food"></param>
    private bool CastSkill(Food food)
    {
        bool isCast = false;
        if (food.Energy >= food.skillInfo.energy)
        {
            float temp = Random.Range(0f, 100f);
            if (temp <= food.skillInfo.change)
            {         
                //Debug.Log(food.foodInfo.AttackForce);
                food.foodView.ShowDamage(food.skillInfo.skillname, Color.yellow);
                food.Energy -= (byte)food.skillInfo.energy;
                food.foodView.SetEnergyView(food.Energy);
                isCast = true;
            }
        }
        return isCast;
    }

    /// <summary>
    /// 处理攻击技能
    /// </summary>
    /// <param name="food"></param>
    private void AttackSkill(Food food, string tag)
    {
        CastSkill(food);
        food.foodInfo.AttackForce = (int)(food.foodInfo.AttackForce * food.skillInfo.value / 100);
        
        food.foodView.PlayerAttackEffect(food.AttackRangeMapGridID, tag);
    }

    /// <summary>
    /// 处理加血技能
    /// </summary>
    /// <param name="food"></param>
    private void HealthSkill(Food food, string tag)
    {
        if (CastSkill(food) == true)
        {
            if (food.skillInfo.skillname == "治疗能量")
            {
                foreach (Food playerfood in TeamManager._instance.PlayerFoods)
                {
                    foreach (int mapgridid in food.AttackRangeMapGridID)
                    {
                        int temp = MapManager._instance.CalcuMapGridID(playerfood.foodPosX, playerfood.foodPosY);

                        if (temp == mapgridid)
                        {
                            playerfood.Energy++;
                            playerfood.foodView.SetEnergyView(food.Energy);
                            Debug.Log(playerfood.foodInfo.FoodName + "能量增加1点");
                        }
                    }
                }
            }
            food.foodInfo.AttackForce = (int)(food.foodInfo.AttackForce * food.skillInfo.value / 100);
            
            food.foodView.HealthEffect(food.AttackRangeMapGridID, tag);
        }
        else
        {
            
            food.foodView.PlayerAttackEffect(food.AttackRangeMapGridID, tag);
        }
    }

    private void PassiveSkill(Food food, string tag)
    {
        CastSkill(food);

        if (food.skillInfo.skillname == "闪避")
        {
            food.dodge -= food.Energy  * 5;
        }
        else if (food.skillInfo.skillname == "充能加血")
        {
            if (food.foodGo.tag == "PlayerFood")
            {
                TeamManager._instance.MaxPlayerHealth += food.Energy * 10;
                HealthInfoView._instance.UpdateHealth();
            }
            else
            {
                TeamManager._instance.MaxEnemyHealth += food.Energy * 10;
                HealthInfoView._instance.UpdateHealth();
            }
        }
        else
        {
            food.foodInfo.AttackForce = (int)(food.foodInfo.AttackForce * food.skillInfo.value / 100) + food.Energy;
        }

        
        food.foodView.PlayerAttackEffect(food.AttackRangeMapGridID, tag);
    }
}
